package com.tony.model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Task {

    private long id;
    private long nums;      //需要执行的数量
    private int playerId;     //队员的id
    private String playName;    //队员的名称

    public int player_start;//0:队员不投票，1：队员投票
    public int corps_start; //0:战队不投票，1：战队投票

    public int corpsId;         //战队id
    public String corpsName;    //战队名称

    private long sTimes;    //开始执行时间
    private long eTimes;    //结束执行时间


    private static String DB_PATH;

    private static Task instances = null;

   public static Task getInstances(String db_path){
       if(instances==null){
           instances=new Task();
       }
       DB_PATH=db_path;
       return instances;
   }


    public Task getData(){
        try {
            long times=System.currentTimeMillis();
            Class.forName("org.sqlite.JDBC");
            Connection conn = DriverManager.getConnection("jdbc:sqlite:"+ DB_PATH);
            Statement stmt = conn.createStatement();
            String sql="SELECT * FROM tb_task WHERE nums > 0 and sTimes <="+times+" AND eTimes >= "+times+" ORDER BY RANDOM() LIMIT 1;";
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                Task task=new Task();
                task.seteTimes(rs.getLong("eTimes"));
                task.setId(rs.getLong("id"));
                task.setPlayerId(rs.getInt("playerId"));
                task.setPlayName(rs.getString("playName"));
                task.setCorpsId(rs.getInt("corpsId"));
                task.setCorpsName(rs.getString("corpsName"));
                task.setNums(rs.getInt("nums"));
                task.setsTimes(rs.getLong("sTimes"));
                task.setPlayer_start(rs.getInt("player_start"));
                task.setCorps_start(rs.getInt("corps_start"));
                rs.close();
                stmt.close();
                conn.close();
                return task;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


    public boolean updateData(Task task){
        try {
            Class.forName("org.sqlite.JDBC");
            Connection conn = DriverManager.getConnection("jdbc:sqlite:"+DB_PATH);
            Statement stmt = conn.createStatement();
            String sql="UPDATE tb_task SET nums = '"+task.getNums()+"' WHERE id="+task.getId();
            stmt.executeUpdate(sql);
            stmt.close();
            conn.close();
            return true;

        }catch (Exception e){

        }
        return false;
    }


    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public long getNums() {
        return nums;
    }

    public void setNums(long nums) {
        this.nums = nums;
    }

    public int getPlayerId() {
        return playerId;
    }

    public void setPlayerId(int playerId) {
        this.playerId = playerId;
    }

    public String getPlayName() {
        return playName;
    }

    public void setPlayName(String playName) {
        this.playName = playName;
    }

    public int getPlayer_start() {
        return player_start;
    }

    public void setPlayer_start(int player_start) {
        this.player_start = player_start;
    }

    public int getCorps_start() {
        return corps_start;
    }

    public void setCorps_start(int corps_start) {
        this.corps_start = corps_start;
    }

    public int getCorpsId() {
        return corpsId;
    }

    public void setCorpsId(int corpsId) {
        this.corpsId = corpsId;
    }

    public String getCorpsName() {
        return corpsName;
    }

    public void setCorpsName(String corpsName) {
        this.corpsName = corpsName;
    }

    public long getsTimes() {
        return sTimes;
    }

    public void setsTimes(long sTimes) {
        this.sTimes = sTimes;
    }

    public long geteTimes() {
        return eTimes;
    }

    public void seteTimes(long eTimes) {
        this.eTimes = eTimes;
    }
}
